package com.example.rbacmanage.controller;

import com.example.rbacmanage.annotation.LogAnnotation;
import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.entity.Menu;
import com.example.rbacmanage.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("/listtree")
    @PreAuthorize("hasAnyAuthority('system:menu:list')")
    public Result getMenuListTree(Menu menu) {
        return Result.success(menuService.getAllTree(menu));
    }

    @GetMapping("/list")
    public Result getMenuList(Menu menu) {
        return Result.success(menuService.getAll(menu));
    }

    @GetMapping("/{id}")
    public Result getMenuById(@PathVariable(value = "id") Long id) {
        return Result.success(menuService.getById(id));
    }

    @PostMapping("/add")
    @LogAnnotation(module = "菜单模块",operation = "新增菜单")
    @PreAuthorize("hasAnyAuthority('system:menu:add')")
    public Result saveMenu(@RequestBody Menu menu) {
        menuService.save(menu);
        return Result.success();
    }

    @DeleteMapping("/remove")
    @LogAnnotation(module = "菜单模块",operation = "删除菜单")
    @PreAuthorize("hasAnyAuthority('system:menu:delete')")
    public Result remove(@RequestBody Long[] ids) {
        menuService.remove(ids);
        return Result.success();
    }
    @PutMapping("/update")
    @LogAnnotation(module = "菜单模块",operation = "修改菜单")
    @PreAuthorize("hasAnyAuthority('system:menu:edit')")
    public Result updateMenu(@RequestBody Menu menu) {
        menuService.update(menu);
        return Result.success();
    }

}
